package com.yjc.hire.server.controller;

import com.alibaba.fastjson.JSON;
import com.yjc.bo.DataDictionaryBO;
import com.yjc.bo.QueryDictItemsBO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.server.VO.CompanyPointsVO;
import com.yjc.hire.server.service.DataDictionaryService;
import com.yjc.pojo.DataDictionary;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/8/9
 * </p>
 */

@RestController
@RequestMapping("dataDict")
public class DataDictionaryController extends BaseInfoProperties {

    @Autowired
    private DataDictionaryService dataDictionaryService;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String DDKEY_PREFIX = DATA_DICTIONARY_LIST_TYPECODE + ":";

    @PostMapping("create")
    public GraceJSONResult createDict(@RequestBody DataDictionaryBO dataDictionary){
        dataDictionaryService.createOrUpdateDataDict(dataDictionary);
        return GraceJSONResult.ok();
    }

    @PostMapping("list")
    public GraceJSONResult listDict(String typeName,String itemValue,
                                    Integer page,Integer limit){
        if (page == null) page = 1;
        if (limit == null) limit = 10;
        PageGridResult<List<DataDictionary>> listPageGridResult =
                dataDictionaryService.pageDataDictionary(typeName, itemValue, page, limit);
        return GraceJSONResult.ok(listPageGridResult);
    }

    @PostMapping("modify")
    public GraceJSONResult modify(@RequestBody DataDictionaryBO dataDictionary){
        if (dataDictionary.getId() == null){
            return GraceJSONResult.error();
        }
        dataDictionaryService.createOrUpdateDataDict(dataDictionary);
        return GraceJSONResult.ok();
    }
    @PostMapping("item")
    public GraceJSONResult item(String dictId){
        if (dictId == null){
            return GraceJSONResult.error();
        }
        return GraceJSONResult.ok(dataDictionaryService.getDataDictionaryById(dictId));
    }

    @PostMapping("delete")
    public GraceJSONResult deleteDict(String dictId) {
        dataDictionaryService.deleteDataDictionaryById(dictId);
        return GraceJSONResult.ok();
    }
        /**
         * 根据字典类型获取所有的字典
         *这个表存储所有的可选数据例如院校类型，公司规模等等
         * @param typeCode
         * @return
         */

        @PostMapping("app/getDataByCode")
        public GraceJSONResult getDataByCode (String typeCode) {
            if (StringUtils.isBlank(typeCode)) return GraceJSONResult.error();
            String ddkey = DDKEY_PREFIX + typeCode;
            String ddList = stringRedisTemplate.opsForValue().get(ddkey);
            List<DataDictionary> list = null;
            if (StringUtils.isNoneBlank(ddList)){
                list = JSON.parseArray(ddList, DataDictionary.class);
            }
            else {
                 list = dataDictionaryService.getDataDictionaryByType(typeCode);
                 stringRedisTemplate.opsForValue().set(ddkey, JSON.toJSONString(list));

            }return GraceJSONResult.ok(list);
        }

        @PostMapping("app/getItemsByKeys")
    public GraceJSONResult getItemsByKeys(@RequestBody QueryDictItemsBO queryDictItemsBO) {
            String[] advantage = queryDictItemsBO.getAdvantage();
            String[] benefits = queryDictItemsBO.getBenefits();
            String[] bonus = queryDictItemsBO.getBonus();
            String[] subsidy = queryDictItemsBO.getSubsidy();
            List<DataDictionary> advantageList = dataDictionaryService.getItemsByKeys(advantage);
            List<DataDictionary> benefitsList = dataDictionaryService.getItemsByKeys(benefits);
            List<DataDictionary> bonusList = dataDictionaryService.getItemsByKeys(bonus);
            List<DataDictionary> subsidylist = dataDictionaryService.getItemsByKeys(subsidy);
            CompanyPointsVO vo = new CompanyPointsVO();
            vo.setAdvantageList(advantageList);
            vo.setBenefitsList(benefitsList);
            vo.setSubsidylist(subsidylist);
            vo.setBonusList(bonusList);
            return GraceJSONResult.ok(vo);

        }

    @PostMapping("app/getItemsByKeys2")
    public GraceJSONResult getItemsByKeys2(@RequestBody QueryDictItemsBO queryDictItemsBO) throws ExecutionException, InterruptedException {
//        String[] advantage = queryDictItemsBO.getAdvantage();
//        String[] benefits = queryDictItemsBO.getBenefits();
//        String[] bonus = queryDictItemsBO.getBonus();
//        String[] subsidy = queryDictItemsBO.getSubsidy();
//        List<DataDictionary> advantageList = dataDictionaryService.getItemsByKeys(advantage);
//        List<DataDictionary> benefitsList = dataDictionaryService.getItemsByKeys(benefits);
//        List<DataDictionary> bonusList = dataDictionaryService.getItemsByKeys(bonus);
//        List<DataDictionary> subsidylist = dataDictionaryService.getItemsByKeys(subsidy);
        CompanyPointsVO vo = new CompanyPointsVO();
        CompletableFuture<List<DataDictionary>> advantageFuture = CompletableFuture.supplyAsync(() -> {
            String advantage[] = queryDictItemsBO.getAdvantage();
            List<DataDictionary> advanges = dataDictionaryService.getItemsByKeys(advantage);
            vo.setAdvantageList(advanges);
            return advanges;
        },threadPoolExecutor);

        CompletableFuture<List<DataDictionary>> benefitsFuture = CompletableFuture.supplyAsync(() ->{
            String[] benefits = queryDictItemsBO.getBenefits();
            List<DataDictionary> benefitsList = dataDictionaryService.getItemsByKeys(benefits);
            vo.setBenefitsList(benefitsList);
            return benefitsList;
        },threadPoolExecutor);

        CompletableFuture<List<DataDictionary>> bonusFuture = CompletableFuture.supplyAsync(() ->{
            String[] bonus = queryDictItemsBO.getBonus();
            List<DataDictionary> bonusList = dataDictionaryService.getItemsByKeys(bonus);
            vo.setBonusList(bonusList);
            return bonusList;
        },threadPoolExecutor);

        CompletableFuture<List<DataDictionary>> subsidyFuture = CompletableFuture.supplyAsync(() ->{
            String[] subsidy = queryDictItemsBO.getSubsidy();
            List<DataDictionary> subsidyList = dataDictionaryService.getItemsByKeys(subsidy);
            vo.setSubsidylist(subsidyList);
            return subsidyList;
        },threadPoolExecutor);

        CompletableFuture<Object> allOfFuture = CompletableFuture.anyOf(
                advantageFuture,
                bonusFuture,
                bonusFuture,
                subsidyFuture
        );
        CompletableFuture.allOf(allOfFuture).get();
        return GraceJSONResult.ok(vo);

    }


}
